﻿using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using OnlineManagement.DBUtility;
using OnlineManagement.IDAL;
namespace OnlineManagement.SQLServerDAL  
{
	 	//Hog
		public partial class Hog: IHog
	{
   		     
		public bool Exists(int HogID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Hog");
			strSql.Append(" where ");
			                                       strSql.Append(" HogID = @HogID  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@HogID", SqlDbType.Int,4)
			};
			parameters[0].Value = HogID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(OnlineManagement.Model.Hog model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Hog(");			
            strSql.Append("HoggeryID,BitID,MotherBitID,PigCategory,PhaseID,PigPenId,TypeID,SourceID,Birthday,DateJoinFarm,IsInFarm,WightJoinFarm,CurrentWight,Account");
			strSql.Append(") values (");
            strSql.Append("@HoggeryID,@BitID,@MotherBitID,@PigCategory,@PhaseID,@PigPenId,@TypeID,@SourceID,@Birthday,@DateJoinFarm,@IsInFarm,@WightJoinFarm,@CurrentWight,@Account");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@HoggeryID", SqlDbType.Int,4) ,            
                        new SqlParameter("@BitID", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@MotherBitID", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@PigCategory", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@PhaseID", SqlDbType.Int,4) ,            
                        new SqlParameter("@PigPenId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SourceID", SqlDbType.TinyInt,1) ,            
                        new SqlParameter("@Birthday", SqlDbType.Date,3) ,            
                        new SqlParameter("@DateJoinFarm", SqlDbType.Date,3) ,            
                        new SqlParameter("@IsInFarm", SqlDbType.Bit,1) ,            
                        new SqlParameter("@WightJoinFarm", SqlDbType.Float,8) ,            
                        new SqlParameter("@CurrentWight", SqlDbType.Float,8) ,            
                        new SqlParameter("@Account", SqlDbType.TinyInt,1)             
              
            };
			            
            parameters[0].Value = model.HoggeryID;                        
            parameters[1].Value = model.BitID;                        
            parameters[2].Value = model.MotherBitID;                        
            parameters[3].Value = model.PigCategory;                        
            parameters[4].Value = model.PhaseID;                        
            parameters[5].Value = Guid.NewGuid();                        
            parameters[6].Value = Guid.NewGuid();                        
            parameters[7].Value = model.SourceID;                        
            parameters[8].Value = model.Birthday;                        
            parameters[9].Value = model.DateJoinFarm;                        
            parameters[10].Value = model.IsInFarm;                        
            parameters[11].Value = model.WightJoinFarm;                        
            parameters[12].Value = model.CurrentWight;                        
            parameters[13].Value = model.Account;                        
			   
			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			if (obj == null)
			{
				return 0;
			}
			else
			{
				                    
            	return Convert.ToInt32(obj);
                                                                  
			}			   
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(OnlineManagement.Model.Hog model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Hog set ");
			                                                
            strSql.Append(" HoggeryID = @HoggeryID , ");                                    
            strSql.Append(" BitID = @BitID , ");                                    
            strSql.Append(" MotherBitID = @MotherBitID , ");                                    
            strSql.Append(" PigCategory = @PigCategory , ");                                    
            strSql.Append(" PhaseID = @PhaseID , ");                                    
            strSql.Append(" PigPenId = @PigPenId , ");                                    
            strSql.Append(" TypeID = @TypeID , ");                                    
            strSql.Append(" SourceID = @SourceID , ");                                    
            strSql.Append(" Birthday = @Birthday , ");                                    
            strSql.Append(" DateJoinFarm = @DateJoinFarm , ");                                    
            strSql.Append(" IsInFarm = @IsInFarm , ");                                    
            strSql.Append(" WightJoinFarm = @WightJoinFarm , ");                                    
            strSql.Append(" CurrentWight = @CurrentWight , ");                                    
            strSql.Append(" Account = @Account  ");            			
			strSql.Append(" where HogID=@HogID ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@HogID", SqlDbType.Int,4) ,            
                        new SqlParameter("@HoggeryID", SqlDbType.Int,4) ,            
                        new SqlParameter("@BitID", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@MotherBitID", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@PigCategory", SqlDbType.NVarChar,20) ,            
                        new SqlParameter("@PhaseID", SqlDbType.Int,4) ,            
                        new SqlParameter("@PigPenId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@TypeID", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SourceID", SqlDbType.TinyInt,1) ,            
                        new SqlParameter("@Birthday", SqlDbType.Date,3) ,            
                        new SqlParameter("@DateJoinFarm", SqlDbType.Date,3) ,            
                        new SqlParameter("@IsInFarm", SqlDbType.Bit,1) ,            
                        new SqlParameter("@WightJoinFarm", SqlDbType.Float,8) ,            
                        new SqlParameter("@CurrentWight", SqlDbType.Float,8) ,            
                        new SqlParameter("@Account", SqlDbType.TinyInt,1)             
              
            };
						            
            parameters[0].Value = model.HogID;                        
            parameters[1].Value = model.HoggeryID;                        
            parameters[2].Value = model.BitID;                        
            parameters[3].Value = model.MotherBitID;                        
            parameters[4].Value = model.PigCategory;                        
            parameters[5].Value = model.PhaseID;                        
            parameters[6].Value = model.PigPenId;                        
            parameters[7].Value = model.TypeID;                        
            parameters[8].Value = model.SourceID;                        
            parameters[9].Value = model.Birthday;                        
            parameters[10].Value = model.DateJoinFarm;                        
            parameters[11].Value = model.IsInFarm;                        
            parameters[12].Value = model.WightJoinFarm;                        
            parameters[13].Value = model.CurrentWight;                        
            parameters[14].Value = model.Account;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int HogID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Hog ");
			strSql.Append(" where HogID=@HogID");
						SqlParameter[] parameters = {
					new SqlParameter("@HogID", SqlDbType.Int,4)
			};
			parameters[0].Value = HogID;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string HogIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Hog ");
			strSql.Append(" where ID in ("+HogIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public OnlineManagement.Model.Hog GetModel(int HogID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select HogID, HoggeryID, BitID, MotherBitID, PigCategory, PhaseID, PigPenId, TypeID, SourceID, Birthday, DateJoinFarm, IsInFarm, WightJoinFarm, CurrentWight, Account  ");			
			strSql.Append("  from Hog ");
			strSql.Append(" where HogID=@HogID");
						SqlParameter[] parameters = {
					new SqlParameter("@HogID", SqlDbType.Int,4)
			};
				return null;
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM Hog ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM Hog ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

   
	}
}

